package com.wocai.jrt.orgadmin.stats.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;

import org.joda.time.DateTime;
import org.joda.time.Months;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wocai.jrt.orgadmin.employee.service.spi.EmployeeService;
import com.wocai.jrt.orgadmin.stats.bean.EmpStatsMapperReqBean;
import com.wocai.jrt.orgadmin.stats.bean.EmpStatsResBean;
import com.wocai.jrt.orgadmin.stats.service.spi.EmployeeStatsService;
import com.wocai.jrt.orgadmin.stats.service.spi.RedisCacheEmpStatsService;
import com.wocai.jrt.orgadmin.utils.UserUtils;

@Named
public class EmployeeStatsServiceImpl implements EmployeeStatsService {
	private static final Logger LOGGER = LoggerFactory.getLogger(EmployeeStatsServiceImpl.class);

	@Inject
	private EmployeeService employeeServiceImpl;
	@Inject
	private RedisCacheEmpStatsService redisCacheEmpStatsServiceImpl;

	@Override
	public List<EmpStatsResBean> queryEmployeeStats(DateTime beginDateTime, DateTime endDateTime,Integer start,Integer pageSize) {
		// 查出所有理财师团队的Employee
		EmpStatsMapperReqBean empStatsMapperReqBean = new EmpStatsMapperReqBean();
		empStatsMapperReqBean.setOrgId(UserUtils.getOrgId());
		empStatsMapperReqBean.setStart(start);
		empStatsMapperReqBean.setPageSize(pageSize);
		List<EmpStatsResBean> employeeList = employeeServiceImpl.queryAllPlanner(empStatsMapperReqBean);

		// 将数据放入map里面
		Map<String, EmpStatsResBean> employeeMap = new HashMap<String, EmpStatsResBean>();
		for (EmpStatsResBean empStatsResBean : employeeList) {
			employeeMap.put(empStatsResBean.getId(), empStatsResBean);
		}
		LOGGER.info("EmployeeStatsServiceImpl queryEmployeeStats, queryAllPlanner [{}]",
				employeeList.size());
		DateTimeFormatter formatter2 = DateTimeFormat.forPattern("yyyy-MM");
		boolean months = beginDateTime.toString(formatter2)
				.equals(endDateTime.toString(formatter2));
		if (months) {// 查询的时间在同一个月里面
			// 投资人数
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				int inerstorSumNum = getInerstorSumNum(empStatsResBean.getId(), beginDateTime,
						endDateTime);
				empStatsResBean.setInvestorNum(inerstorSumNum);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 成交投资人数
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				int dealInvestorNum = getDealInerstorSumNum(empStatsResBean.getId(), beginDateTime,
						endDateTime);
				empStatsResBean.setDealInvestorNum(dealInvestorNum);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 客户转化率
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				double investorTurnRate = 0D;
				if (empStatsResBean.getDealInvestorNum() != 0) {
					investorTurnRate = (double) empStatsResBean.getDealInvestorNum()
							/ (double) empStatsResBean.getInvestorNum();
				}
				empStatsResBean.setInvestorTurnRate(investorTurnRate);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 预约数
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				int appointmentSumNum = getAppointmentSumNum(empStatsResBean.getId(),
						beginDateTime, endDateTime);
				empStatsResBean.setAppointmentNum(appointmentSumNum);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 外勤签到数
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				int checkinNum = getCheckinSumNum(empStatsResBean.getId(), beginDateTime,
						endDateTime);
				empStatsResBean.setCheckinNum(checkinNum);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 总订单数
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				int ordersTotalNum = getOrdersTotalNum(empStatsResBean.getId(), beginDateTime,
						endDateTime);
				empStatsResBean.setOrdersTotalNum(ordersTotalNum);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 成交订单数
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				int ordersNum = getOrderslNum(empStatsResBean.getId(), beginDateTime, endDateTime);
				empStatsResBean.setOrdersNum(ordersNum);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 订单转化率
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				double ordersTurnRate = 0D;
				if (empStatsResBean.getOrdersTotalNum() != 0) {
					ordersTurnRate = (double) empStatsResBean.getOrdersNum()
							/ (double) empStatsResBean.getOrdersTotalNum();
				}
				empStatsResBean.setOrdersTurnRate(ordersTurnRate);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 销售额
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				double ordersAmount = getOrdersAmount(empStatsResBean.getId(), beginDateTime,
						endDateTime);
				empStatsResBean.setOrdersAmount(ordersAmount);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 客单价
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				double investorUnitPrice = 0D;
				if (empStatsResBean.getOrdersNum() != 0) {
					investorUnitPrice = empStatsResBean.getOrdersAmount()
							/ (double) empStatsResBean.getOrdersNum();
				}
				empStatsResBean.setInvestorUnitPrice(investorUnitPrice);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}

		} else {// 查询的时间分别在两个月里面
			DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM");
			// 后一个月的开始时间
			DateTime tempBeginDateTime = DateTime.parse(endDateTime.toString(formatter), formatter);
			// 前一个月的结束时间
			DateTime tempEndDateTime = tempBeginDateTime.minusMillis(1);

			// 投资人数
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				int inerstorSumNum = getInerstorSumNum(empStatsResBean.getId(), beginDateTime,
						tempEndDateTime)
						+ getInerstorSumNum(empStatsResBean.getId(), tempBeginDateTime, endDateTime);
				empStatsResBean.setInvestorNum(inerstorSumNum);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 成交投资人数
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				int dealInvestorNum = getDealInerstorSumNum(empStatsResBean.getId(), beginDateTime,
						tempEndDateTime)
						+ getDealInerstorSumNum(empStatsResBean.getId(), tempBeginDateTime,
								endDateTime);
				empStatsResBean.setDealInvestorNum(dealInvestorNum);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 客户转化率
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				double investorTurnRate = 0D;
				if (empStatsResBean.getDealInvestorNum() != 0) {
					investorTurnRate = (double) empStatsResBean.getDealInvestorNum()
							/ (double) empStatsResBean.getInvestorNum();
				}
				empStatsResBean.setInvestorTurnRate(investorTurnRate);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 预约数
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				int appointmentSumNum = getAppointmentSumNum(empStatsResBean.getId(),
						beginDateTime, tempEndDateTime)
						+ getAppointmentSumNum(empStatsResBean.getId(), tempBeginDateTime,
								endDateTime);
				empStatsResBean.setAppointmentNum(appointmentSumNum);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 外勤签到数
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				int checkinNum = getCheckinSumNum(empStatsResBean.getId(), beginDateTime,
						tempEndDateTime)
						+ getCheckinSumNum(empStatsResBean.getId(), tempBeginDateTime, endDateTime);
				empStatsResBean.setCheckinNum(checkinNum);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 总订单数
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				int ordersTotalNum = getOrdersTotalNum(empStatsResBean.getId(), beginDateTime,
						tempEndDateTime)
						+ getOrdersTotalNum(empStatsResBean.getId(), tempBeginDateTime, endDateTime);
				empStatsResBean.setOrdersTotalNum(ordersTotalNum);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 成交订单数
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				int ordersNum = getOrderslNum(empStatsResBean.getId(), beginDateTime,
						tempEndDateTime)
						+ getOrderslNum(empStatsResBean.getId(), tempBeginDateTime, endDateTime);
				empStatsResBean.setOrdersNum(ordersNum);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 订单转化率
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				double ordersTurnRate = 0D;
				if (empStatsResBean.getOrdersTotalNum() != 0) {
					ordersTurnRate = (double) empStatsResBean.getOrdersNum()
							/ (double) empStatsResBean.getOrdersTotalNum();
				}
				empStatsResBean.setOrdersTurnRate(ordersTurnRate);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 销售额
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				double ordersAmount = getOrdersAmount(empStatsResBean.getId(), beginDateTime,
						tempEndDateTime)
						+ getOrdersAmount(empStatsResBean.getId(), tempBeginDateTime, endDateTime);
				empStatsResBean.setOrdersAmount(ordersAmount);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
			// 客单价
			for (String key : employeeMap.keySet()) {
				EmpStatsResBean empStatsResBean = employeeMap.get(key);
				double investorUnitPrice = 0D;
				if (empStatsResBean.getOrdersNum() != 0) {
					investorUnitPrice = empStatsResBean.getOrdersAmount()
							/ (double) empStatsResBean.getOrdersNum();
				}
				empStatsResBean.setInvestorUnitPrice(investorUnitPrice);
				employeeMap.put(empStatsResBean.getId(), empStatsResBean);
			}
		}

		List<EmpStatsResBean> resultList = new ArrayList<EmpStatsResBean>();
		for (int i = 0; i < employeeList.size(); i++) {
			resultList.add(employeeMap.get(employeeList.get(i).getId()));
		}
		return resultList;
	}

	private double getOrdersAmount(String id, DateTime beginDateTime, DateTime endDateTime) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM");
		List<Double> ordersAmount = redisCacheEmpStatsServiceImpl.queryOrdersAmountList(id,
				beginDateTime.toString(formatter));
		int beginDay = beginDateTime.getDayOfMonth();
		int endDay = endDateTime.getDayOfMonth();
		double num = 0D;
		for (int i = beginDay; i <= endDay; i++) {
			num += ordersAmount.get(i - 1);
		}
		return num;
	}

	private int getOrderslNum(String id, DateTime beginDateTime, DateTime endDateTime) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM");
		List<Integer> ordersNum = redisCacheEmpStatsServiceImpl.queryOrdersNumList(id,
				beginDateTime.toString(formatter));
		int beginDay = beginDateTime.getDayOfMonth();
		int endDay = endDateTime.getDayOfMonth();
		int num = 0;
		for (int i = beginDay; i <= endDay; i++) {
			num += ordersNum.get(i - 1);
		}
		return num;
	}

	private int getOrdersTotalNum(String id, DateTime beginDateTime, DateTime endDateTime) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM");
		List<Integer> ordersTotalNum = redisCacheEmpStatsServiceImpl.queryOrdersTotalNumList(id,
				beginDateTime.toString(formatter));
		int beginDay = beginDateTime.getDayOfMonth();
		int endDay = endDateTime.getDayOfMonth();
		int num = 0;
		for (int i = beginDay; i <= endDay; i++) {
			num += ordersTotalNum.get(i - 1);
		}
		return num;
	}

	private int getCheckinSumNum(String id, DateTime beginDateTime, DateTime endDateTime) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM");
		List<Integer> checkinNumList = redisCacheEmpStatsServiceImpl.queryCheckinNumList(id,
				beginDateTime.toString(formatter));
		int beginDay = beginDateTime.getDayOfMonth();
		int endDay = endDateTime.getDayOfMonth();
		int num = 0;
		for (int i = beginDay; i <= endDay; i++) {
			num += checkinNumList.get(i - 1);
		}
		return num;
	}

	/**
	 * 统计这段时间新加的投资人有多少人是产生了订单
	 * 
	 * @param id
	 * @param beginDateTime
	 * @param endDateTime
	 * @return
	 */
	private int getDealInerstorSumNum(String id, DateTime beginDateTime, DateTime endDateTime) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM");
		List<List<String>> investorNumList = redisCacheEmpStatsServiceImpl.queryInvestorNumList(id,
				beginDateTime.toString(formatter));
		int beginDay = beginDateTime.getDayOfMonth();
		int endDay = endDateTime.getDayOfMonth();
		// 这几天添加的投资人id
		List<String> investorList = new ArrayList<String>();
		for (int i = beginDay; i <= endDay; i++) {
			investorList.addAll(investorNumList.get(i - 1));
		}
		// 所有有订单的投资人id
		List<String> dealInvestorList = redisCacheEmpStatsServiceImpl.queryDealInvestorList(id);
		investorList.retainAll(dealInvestorList);
		return investorList.size();
	}

	/**
	 * 统计这段时间内预约总数,要求是开始时间和结束时间再同一个月
	 * 
	 * @param empStatsResBean
	 * @param beginDateTime
	 * @param endDateTime
	 * @return
	 */
	private int getAppointmentSumNum(String id, DateTime beginDateTime, DateTime endDateTime) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM");
		List<Integer> appointmentNumList = redisCacheEmpStatsServiceImpl.queryAppointmentNumList(
				id, beginDateTime.toString(formatter));
		int beginDay = beginDateTime.getDayOfMonth();
		int endDay = endDateTime.getDayOfMonth();
		int num = 0;
		for (int i = beginDay; i <= endDay; i++) {
			num += appointmentNumList.get(i - 1);
		}
		return num;
	}

	/**
	 * 统计这段时间内投资人总数,要求是开始时间和结束时间再同一个月
	 * 
	 * @param empStatsResBean
	 * @param beginDateTime
	 * @param endDateTime
	 * @return
	 */
	private int getInerstorSumNum(String id, DateTime beginDateTime, DateTime endDateTime) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM");
		List<List<String>> investorNumList = redisCacheEmpStatsServiceImpl.queryInvestorNumList(id,
				beginDateTime.toString(formatter));
		int beginDay = beginDateTime.getDayOfMonth();
		int endDay = endDateTime.getDayOfMonth();
		int num = 0;
		for (int i = beginDay; i <= endDay; i++) {
			num += investorNumList.get(i - 1).size();
		}
		return num;
	}

	@Override
	public List<EmpStatsResBean> queryEmployeeStatsMonth(DateTime beginDateTime,
			DateTime endDateTime,Integer start,Integer pageSize) {
		// 查出所有理财师团队的Employee
		EmpStatsMapperReqBean empStatsMapperReqBean = new EmpStatsMapperReqBean();
		empStatsMapperReqBean.setOrgId(UserUtils.getOrgId());
		empStatsMapperReqBean.setStart(start);
		empStatsMapperReqBean.setPageSize(pageSize);
		List<EmpStatsResBean> employeeList = employeeServiceImpl.queryAllPlanner(empStatsMapperReqBean);
		// 将数据放入map里面
		Map<String, EmpStatsResBean> employeeMap = new HashMap<String, EmpStatsResBean>();
		for (EmpStatsResBean empStatsResBean : employeeList) {
			employeeMap.put(empStatsResBean.getId(), empStatsResBean);
		}
		LOGGER.info("EmployeeStatsServiceImpl queryEmployeeStats, queryAllPlanner [{}]",
				employeeList.size());

		DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM");
		beginDateTime = DateTime.parse(beginDateTime.toString(formatter), formatter);
		endDateTime = DateTime.parse(endDateTime.toString(formatter), formatter);
		int months = Months.monthsBetween(beginDateTime, endDateTime).getMonths();
		// 投资人数,将每个月的人数加起来
		for (String key : employeeMap.keySet()) {
			EmpStatsResBean empStatsResBean = employeeMap.get(key);
			int inerstorSumNum = 0;
			DateTime tempdaDateTime = beginDateTime;
			for (int i = 0; i <= months; i++) {
				List<List<String>> investorNumList = redisCacheEmpStatsServiceImpl
						.queryInvestorNumList(empStatsResBean.getId(),
								tempdaDateTime.toString(formatter));
				for (int j = 0; j < investorNumList.size(); j++) {
					inerstorSumNum = inerstorSumNum + investorNumList.get(j).size();
				}
				tempdaDateTime = tempdaDateTime.plusMonths(1);
			}
			empStatsResBean.setInvestorNum(inerstorSumNum);
			employeeMap.put(empStatsResBean.getId(), empStatsResBean);
		}
		// 成交投资人数
		for (String key : employeeMap.keySet()) {
			EmpStatsResBean empStatsResBean = employeeMap.get(key);
			// 所有有订单的投资人
			List<String> dealInvestorList = redisCacheEmpStatsServiceImpl
					.queryDealInvestorList(empStatsResBean.getId());
			DateTime tempdaDateTime = beginDateTime;
			// 这段时间添加的投资人
			List<String> investorList = new ArrayList<String>();
			for (int i = 0; i <= months; i++) {
				List<List<String>> investorNumList = redisCacheEmpStatsServiceImpl
						.queryInvestorNumList(empStatsResBean.getId(),
								tempdaDateTime.toString(formatter));
				for (int j = 0; j < investorNumList.size(); j++) {
					investorList.addAll(investorNumList.get(j));
				}
				tempdaDateTime = tempdaDateTime.plusMonths(1);
			}
			int dealInvestorNum = 0;
			// 求交集
			investorList.retainAll(dealInvestorList);
			dealInvestorNum = investorList.size();
			empStatsResBean.setDealInvestorNum(dealInvestorNum);
			employeeMap.put(empStatsResBean.getId(), empStatsResBean);
		}
		// 客户转化率
		for (String key : employeeMap.keySet()) {
			EmpStatsResBean empStatsResBean = employeeMap.get(key);
			double investorTurnRate = 0D;
			if (empStatsResBean.getDealInvestorNum() != 0) {
				investorTurnRate = (double) empStatsResBean.getDealInvestorNum()
						/ (double) empStatsResBean.getInvestorNum();
			}
			empStatsResBean.setInvestorTurnRate(investorTurnRate);
			employeeMap.put(empStatsResBean.getId(), empStatsResBean);
		}
		// 预约数
		for (String key : employeeMap.keySet()) {
			EmpStatsResBean empStatsResBean = employeeMap.get(key);
			int appointmentSumNum = 0;
			DateTime tempdaDateTime = beginDateTime;
			for (int i = 0; i <= months; i++) {
				List<Integer> appointmentNumList = redisCacheEmpStatsServiceImpl
						.queryAppointmentNumList(empStatsResBean.getId(),
								tempdaDateTime.toString(formatter));
				for (int j = 0; j < appointmentNumList.size(); j++) {
					appointmentSumNum = appointmentSumNum + appointmentNumList.get(j);
				}
				tempdaDateTime = tempdaDateTime.plusMonths(1);
			}
			empStatsResBean.setAppointmentNum(appointmentSumNum);
			employeeMap.put(empStatsResBean.getId(), empStatsResBean);
		}
		// 外勤签到数
		for (String key : employeeMap.keySet()) {
			EmpStatsResBean empStatsResBean = employeeMap.get(key);
			int checkinNum = 0;
			DateTime tempDateTime = beginDateTime;
			for (int i = 0; i <= months; i++) {
				List<Integer> checkinNumList = redisCacheEmpStatsServiceImpl.queryCheckinNumList(
						empStatsResBean.getId(), tempDateTime.toString(formatter));
				for (int j = 0; j < checkinNumList.size(); j++) {
					checkinNum = checkinNum + checkinNumList.get(j);
				}
				tempDateTime = tempDateTime.plusMonths(1);
			}
			empStatsResBean.setCheckinNum(checkinNum);
			employeeMap.put(empStatsResBean.getId(), empStatsResBean);
		}
		// 总订单数
		for (String key : employeeMap.keySet()) {
			EmpStatsResBean empStatsResBean = employeeMap.get(key);
			int ordersTotalNum = 0;
			DateTime tempDateTime = beginDateTime;
			for (int i = 0; i <= months; i++) {
				List<Integer> ordersTotalNumList = redisCacheEmpStatsServiceImpl
						.queryOrdersTotalNumList(empStatsResBean.getId(),
								tempDateTime.toString(formatter));
				for (int j = 0; j < ordersTotalNumList.size(); j++) {
					ordersTotalNum = ordersTotalNum + ordersTotalNumList.get(j);
				}
				tempDateTime = tempDateTime.plusMonths(1);
			}
			empStatsResBean.setOrdersTotalNum(ordersTotalNum);
			employeeMap.put(empStatsResBean.getId(), empStatsResBean);
		}
		// 成交订单数
		for (String key : employeeMap.keySet()) {
			EmpStatsResBean empStatsResBean = employeeMap.get(key);
			int ordersNum = 0;
			DateTime tempDateTime = beginDateTime;
			for (int i = 0; i <= months; i++) {
				List<Integer> ordersNumList = redisCacheEmpStatsServiceImpl.queryOrdersNumList(
						empStatsResBean.getId(), tempDateTime.toString(formatter));
				for (int j = 0; j < ordersNumList.size(); j++) {
					ordersNum = ordersNum + ordersNumList.get(j);
				}
				tempDateTime = tempDateTime.plusMonths(1);
			}
			empStatsResBean.setOrdersNum(ordersNum);
			employeeMap.put(empStatsResBean.getId(), empStatsResBean);
		}
		// 订单转化率
		for (String key : employeeMap.keySet()) {
			EmpStatsResBean empStatsResBean = employeeMap.get(key);
			double ordersTurnRate = 0D;
			if (empStatsResBean.getOrdersTotalNum() != 0) {
				ordersTurnRate = (double) empStatsResBean.getOrdersNum()
						/ (double) empStatsResBean.getOrdersTotalNum();
			}
			empStatsResBean.setOrdersTurnRate(ordersTurnRate);
			employeeMap.put(empStatsResBean.getId(), empStatsResBean);
		}
		// 销售额
		for (String key : employeeMap.keySet()) {
			EmpStatsResBean empStatsResBean = employeeMap.get(key);
			double ordersAmount = 0D;
			DateTime tempDateTime = beginDateTime;
			for (int i = 0; i <= months; i++) {
				List<Double> ordersAmountList = redisCacheEmpStatsServiceImpl.queryOrdersAmountList(empStatsResBean.getId(), tempDateTime.toString(formatter));
				for (int j = 0; j < ordersAmountList.size(); j++) {
					ordersAmount = ordersAmount + ordersAmountList.get(j);
				}
				tempDateTime = tempDateTime.plusMonths(1);
			}
			empStatsResBean.setOrdersAmount(ordersAmount);
			employeeMap.put(empStatsResBean.getId(), empStatsResBean);
		}
		// 客单价
		for (String key : employeeMap.keySet()) {
			EmpStatsResBean empStatsResBean = employeeMap.get(key);
			double investorUnitPrice = 0D;
			if (empStatsResBean.getOrdersNum() != 0) {
				investorUnitPrice = empStatsResBean.getOrdersAmount()
						/ (double) empStatsResBean.getOrdersNum();
			}
			empStatsResBean.setInvestorUnitPrice(investorUnitPrice);
			employeeMap.put(empStatsResBean.getId(), empStatsResBean);
		}

		List<EmpStatsResBean> resultList = new ArrayList<EmpStatsResBean>();
		for (int i = 0; i < employeeList.size(); i++) {
			resultList.add(employeeMap.get(employeeList.get(i).getId()));
		}
		return resultList;
	}

	@Override
	public long queryEmployeeStatsCount(DateTime beginDateTime, DateTime endDateTime) {
		EmpStatsMapperReqBean empStatsMapperReqBean = new EmpStatsMapperReqBean();
		empStatsMapperReqBean.setOrgId(UserUtils.getOrgId());
		empStatsMapperReqBean.setStart(null);
		empStatsMapperReqBean.setPageSize(null);
		List<EmpStatsResBean> employeeList = employeeServiceImpl.queryAllPlanner(empStatsMapperReqBean);
		return employeeList.size();
	}
}
